package mall.generator;

import java.util.Map;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
public class StringUtils {
    /**
     * 转小驼峰命名
     * 例如: user_name -> userName, USER_NAME -> userName
     */
    public static String toLowerCamelCase(String str) {
        if (str == null || str.trim().isEmpty()) {
            return str;
        }

        // 统一转换为小写并用下划线分割
        String[] parts = str.toLowerCase().split("_");
        StringBuilder result = new StringBuilder(parts[0]);

        for (int i = 1; i < parts.length; i++) {
            if (!parts[i].isEmpty()) {
                result.append(parts[i].substring(0, 1).toUpperCase())
                        .append(parts[i].substring(1));
            }
        }

        return result.toString();
    }

    /**
     * 转大驼峰命名
     * userName -> UserName
     */
    public static String toUpperCamelCase(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }

        // 移除字符串两端的空白字符
        str = str.trim();
        if (str.isEmpty()) {
            return str;
        }

        // 直接将首字母大写
        return Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }


    /**
     * 格式化字符串，使用 Map 替换占位符
     * 例如：format("Hello {name}, you are {age} years old", map)
     */
    public static String format(String template, Map<String, Object> params) {
        if (template == null || template.isEmpty()) {
            return template;
        }

        if (params == null || params.isEmpty()) {
            return template;
        }

        String result = template;
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            String placeholder = "{" + key + "}";
            result = result.replace(placeholder, value != null ? value.toString() : "");
        }

        return result;
    }
}
